const mongoose = require('mongoose')

const field = { uuid: 1, createdAt: 1 }

let clientSchema = new mongoose.Schema({
  uuid: { type: String, unique: true },
  platform: String,
  device: String,
  externalIp: String,
  publicIp: String,
  createdAt: String,
})

let Client = mongoose.model('Client', clientSchema)

Client.findUuidByRegex = function(pattern) {
  return this.find({ createdAt: pattern })
    .select(field)
    .exec()
}

Client.findUuidByRange = function(createdAtRange) {
  return this.find({
    createdAt: {
      $gte: createdAtRange.start + ' 00:00:00',
      $lte: createdAtRange.end + ' 23:59:59',
    },
  })
  .select(field)
  .exec()
}

module.exports = Client
